package com.duolaibei.business.view.goods;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.utils.inject.Inject;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class UnitsManegeActivity extends BaseActivity {

    @Inject(R.id.recycle_units)
    private RecyclerView mRecycleUnits;
    @Inject(R.id.back)
    private RelativeLayout mBack;
    @Inject(R.id.title)
    private TextView mtxtTtile;

    private List<String> mUnitsList;
    private UnitsAdapter unitsAdapter;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_goods_units);
    }

    @Override
    protected void initViews() {
        mtxtTtile.setText("基本单位");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        initUnitsData();
        setUnitlsAdapter();
    }

    @Override
    protected void onSetListener() {
        mBack.setOnClickListener(getThis());
        unitsAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent();
                intent.putExtra("unit_goods", mUnitsList.get(position));
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }

    private void initUnitsData() {
        mUnitsList = new ArrayList<>();
        String[] units = new String[]{
                "克",
                "千克",
                "斤",
                "两",
                "个",
                "件",
                "包",
                "条",
                "袋",
                "箱",
                "盒",
                "双",
                "套",
                "台",
                "把",
                "码",
                "块",
                "瓶",
                "本",
                "对",
                "只"
        };
        mUnitsList.addAll(Arrays.asList(units));
    }

    private void setUnitlsAdapter() {
        unitsAdapter = new UnitsAdapter(getThis(), R.layout.item_units_manage, mUnitsList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getThis());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleUnits.setLayoutManager(layoutManager);
        Drawable drawable = ContextCompat.getDrawable(Objects.requireNonNull(getThis()), R.drawable.shape_recycle_div);
        DividerItemDecoration decoration = new DividerItemDecoration(Objects.requireNonNull(getThis()), DividerItemDecoration.VERTICAL);
        if (drawable != null) {
            decoration.setDrawable(drawable);
            mRecycleUnits.addItemDecoration(decoration);
        } else
            mRecycleUnits.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getThis()), DividerItemDecoration.VERTICAL));
        mRecycleUnits.setAdapter(unitsAdapter);
    }

    private class UnitsAdapter extends CommonAdapter<String> {

        public UnitsAdapter(Context context, int layoutId, List<String> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, String s, int position) {
            holder.setText(R.id.unit_name, s);
        }
    }
}
